/**
 * @(#)PageStaticAction.java 2009-12-17
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.actions;

import java.io.IOException;
import java.io.PrintWriter;
import net.sf.json.JSONObject;
import com.wilmar.common.util.GenerateRandomNumber;
import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.core.services.api.IProcessPageStaticService;
import com.wilmar.process.entity.ProcessInformation;
import com.wilmar.process.entity.ProcessPageStatic;
import com.wilmar.process.entity.vo.ProcessPageStaticVo;

/**
 * @author asus Email:zhoulibo_java@msn.cn
 * @version 1.0.0
 */
public class PageStaticAction extends ProcessActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IProcessPageStaticService processPageStaticService;

	private ProcessPageStaticVo pageStaticVo;
	private String pageStaticJSON;

	public String getPageStaticJSON() {
		return pageStaticJSON;
	}

	public void setPageStaticJSON(String pageStaticJSON) {
		this.pageStaticJSON = pageStaticJSON;
	}

	public static final String FILE_SUFFIX = ".html";

	public ProcessInformation getProcessInformation() {
		return processInformation;
	}

	public void setProcessInformation(ProcessInformation processInformation) {
		this.processInformation = processInformation;
	}

	private void outString(String str) {
		this.response.setContentType("application/json;charset=utf-8");
		this.response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = this.response.getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

	public void pageStaticAjax() throws IOException {
		if (this.pageStaticJSON != null) {
			// newData = URLDecoder.decode(data, "UTF-8");
			JSONObject jsonObject = JSONObject.fromObject(pageStaticJSON);
			this.pageStaticVo = (ProcessPageStaticVo) JSONObject.toBean(
					jsonObject, ProcessPageStaticVo.class);
		}
		String realPath = request.getSession().getServletContext().getRealPath(
				ProcessSystemDeclareKeys.SLASH);
		String fileName = GenerateRandomNumber.getRadomNumber() + FILE_SUFFIX;
		String fileDir = realPath
				+ ProcessSystemDeclareKeys.SYSTEM_DEFAULT_NAMESPACE
				+ ProcessSystemDeclareKeys.PROCESS_PAGE_STATIC_DIR;
		String fileUrl = realPath
				+ ProcessSystemDeclareKeys.SYSTEM_DEFAULT_NAMESPACE
				+ ProcessSystemDeclareKeys.PROCESS_PAGE_STATIC_DIR + fileName;
		String fileSaveUrl = ProcessSystemDeclareKeys.PROCESS_PAGE_STATIC_DIR
				+ fileName;
		ProcessPageStatic processPageStatic = new ProcessPageStatic();
		if (this.pageStaticVo != null) {
			this.processPageStaticService.writeHtmlPage(fileDir, fileUrl,
					this.pageStaticVo.getHtml().getBytes(
							ProcessSystemDeclareKeys.SYSTEM_DEFAULT_CODE_SET));
			processPageStatic.setHistoryTaskId(this.pageStaticVo
					.getTaskInstanceId());
		}
		processPageStatic.setHtmlName(fileName);
		processPageStatic.setHtmlUrl(fileSaveUrl);
		this.processPageStaticService.savePageStaticInfo(processPageStatic);
		this.outString("sucess");
	}

	public IProcessPageStaticService getProcessPageStaticService() {
		return processPageStaticService;
	}

	public void setProcessPageStaticService(
			IProcessPageStaticService processPageStaticService) {
		this.processPageStaticService = processPageStaticService;
	}

	public ProcessPageStaticVo getPageStaticVo() {
		return pageStaticVo;
	}

	public void setPageStaticVo(ProcessPageStaticVo pageStaticVo) {
		this.pageStaticVo = pageStaticVo;
	}

}
